/**
 * Created by lenovo on 2017/9/18.
 */
(function() {
    size();
    window.onresize = function() {
        size();
    }

    function size() {
        var winW = document.documentElement.clientWidth || document.body.clientWidth;
        document.documentElement.style.fontSize = (winW / 320) * 100 + "px";
    }
})()



	var img_url="http://47.92.37.168/supermarket/img/";
	var username=sessionStorage.getItem('username');
//	 var num=sessionStorage.getItem('num');
var  num=Number(sessionStorage.getItem('num'));
//var num=0;
	var classify=null;
	var start=0; 
	var flag_update=true; 
	map(num);
function map(num){
    switch (num){   
        case 0:
            classify='muying';
            break;
        case 1:
            classify='lingshi';
            break;
        case 2:
            classify='jiushui';
            break;
        case 3:
            classify='liangyou';
            break;
        case 4:
            classify='meirong';
            break;
        case 5:
            classify='jingxuan';
            break;
        case 6:
            classify='jiaqing';
            break;
        case 7:
            classify='jiayong';
            break;
        case 8:
            classify='shengxian';
            break;
        case 9:
            classify='jiayong';
            break;
    }
}
getData(start,classify);
function getData (start,classify) {
    $.ajax({
        type:"post"
        ,url:"http://47.92.37.168/supermarket/data/get_commodity.php"
        ,async:true
        ,data:{
            'start':start,
            'classify':classify
        }
        ,success:function(data){
            console.log(data);
               if (data) {
                   for (var i=0;i<data.length;i++) {
                       $('.swiper2 .swiper-slide').eq(num).find('ul').append('<li id="'+data[i].id+'"><a><img src="'+img_url+data[i].img+'" alt="" class="img"><p>'+data[i].name+'</p><div class="div"><div class="zuo"><span>月销'+data[i].count+'件</span><h3>￥'+data[i].price+'</h3></div><div class="you"><span class="fa fa-shopping-cart" style="color:#fff;"></span></div></div></a></li>');
                   }
                   
                   var aLi=$('.swiper2 .swiper-slide').eq(num).find('ul').children();
                   for (var i=0;i<aLi.length;i++) {
                       if (i%2==0) {
                           aLi.eq(i).addClass('border_right');
                       }
                   }
                   
                   if(data.length<4){
                       flag_update=false;
                   }
                   
               } else{
                   flag_update=false;
               }
 
        }
    });
}
    //向上滑动更新数据
	$(document).on('touchend',function(e){
		start=4;
		if ( $(this).scrollTop()==$(this).height()-$(window).height() && flag_update ) {
			
			start += 4;
			getData(start,classify);
		}
	})
//点击导航菜单
//	$('.swiper1 .swiper-slide').click(function(ev){
//		var e=ev||window.event;
//		if (e.clientX<60) {
//			$('.list-product').scrollLeft($('.list-product').scrollLeft()-70);
//		}
//		if (e.clientX>$(window).width()-60) {
//			$('.list-product').scrollLeft($('.list-product').scrollLeft()+70);
//		}

var swiper1 = new Swiper('.swiper1', {
    slidesPerView:5,
    onTap: function() {//添加点击事件
        //获取当前点击按钮的下标
        var btnIndex = swiper1.clickedIndex;
        num=btnIndex;
        map(num) //改变classify的值
        //修改样式，绑定active
        $('.swiper1 .swiper-slide').eq(btnIndex).addClass('active').siblings().removeClass('active');
        //控制swiper2滑动
        start=0;
        flag_update=true;
       // $(".swiper2 .swiper-slide ul").empty()
        swiper2.slideTo(btnIndex);
        getData(start,classify);
    }


})
var swiper2 = new Swiper('.swiper2', {


    onSlideChangeStart: function() { //当slide开始滑动时触发
        //获取当前滑动到的页面下标
        var slideIndex = swiper2.activeIndex;


        //控制swiper1  滚动
        $('.swiper1 .swiper-slide').eq(slideIndex).addClass('active').siblings().removeClass('active');
        swiper1.slideTo(slideIndex);
        num=slideIndex
        map(num) //改变classify的值
        start=0;
        flag_update=true;
       // $(".swiper2 .swiper-slide ul").empty()
        getData(start,classify);
        
    }
})
	//var swiper1 = new Swiper('.swiper1', {
//  slidesPerView: 5,
//  onTap: function() { //添加点击事件
//      //获取当前点击按钮的下标
//       btnIndex = swiper1.clickedIndex;
//     
// //	swiper2.slideTo(actIndex);
//		num=btnIndex;
//		start=0;
////		flag_update=true;
//      //修改样式，绑定active
//      $('.swiper1 .swiper-slide').eq(btnIndex).addClass('active').siblings().removeClass('active');
//      //控制swiper2滑动
//      swiper2.slideTo(btnIndex);
//       	
//     
// 		 $('.swiper2 .swiper-slide').eq(num).find('ul').empty();
//			map(num);//改变classify的值
//
//		getData(start,classify);
//
//  },
//
//})
//var swiper2 = new Swiper('.swiper2', {
//  onSlideChangeStart: function() { //当slide开始滑动时触发
//      //获取当前滑动到的页面下标
//     slideIndex = swiper2.activeIndex;
//     swiper1.slideTo(btnIndex);
//		 num= slideIndex;
//	
//		start=0;
//		flag_update=true;	
//      //控制swiper1  滚动
//      $('.swiper1 .swiper-slide').eq(slideIndex).addClass('active').siblings().removeClass('active');
//      swiper1.slideTo(slideIndex);
// 		 $('.swiper2 .swiper-slide').eq(num).find('ul').empty();
//		map(num);//改变classify的值
//
//		getData(start,classify);
//      
//  },
//	onSlideChangeEnd:function(){
//		var num=swiper2.activeIndex;
//		console.log(num);
//				//console.log(swiper1.clickedIndex)
//		start=0;
//		flag_update=true;
////		$('.nav_main a').removeClass('current');
////		$(this).children('a').addClass('current');
//		//num=$(this).btnIndex;
//		map(num);//改变classify的值
////		$('.pro_lb').animate({'marginLeft':-num*16+'rem'},500,function(){
////			$('.list').empty();
////		});
//		getData(start,classify);
//		//console.log(e.clientX,$(window).width(),$(this).index(),$('.list-product').scrollLeft());
//	//})
//	
//
//	}
    
//})
//})

//点击产品跳转到详情页
	$(document).on('click','.list img',function(){
		sessionStorage.setItem('pro_id',$(this).parents('li').attr('id'));
		
		console.log($(this).parents('li').attr('id'))
		location.href="details.html";
	})
	




var scrollTop = scrollTop2 =null;



$(document).on('touchstart',function(e){
    scrollTop=$(document).scrollTop();
})
$(document).on('touchmove',function(e){
    scrollTop2=$(document).scrollTop();
    if ( scrollTop2 > scrollTop ) {
        $('header').stop().animate({'top':'-45px'},500);
    }else{
        $('header').stop().animate({'top':'0px'},500);
    }
})
//购物车
$('.cart').click(function(){
		if (username) {
			location.href="shopping.html";
		} else{
			location.href="login.html";
		}
})

//点击购物车
$(document).on('click','.you',function(e){
		stopPro(e);//阻止冒泡
//		var left=$('.cart').offset().left;//距离文档左侧的距离
//		var top=$('.cart').offset().top;//距离文档顶部的距离
		var _this=$(this);
		if (username) {
			$.ajax({
				type:"get"
				,url:"http://47.92.37.168/supermarket/data/my_commodity_car.php"
				,async:true
				,data:{
					'user_phone':username,
					'commodity_id':$(this).parents('li').attr('id'),
					'count':1
				}
				,dataType:"jsonp"
				,jsonp:"callback"
				,success:function(data){
					console.log(data);
					if (data.msg=='success') {
						//图片移动效果
						var left=$('.cart').offset().left;//距离文档左侧的距离
						var top=$('.cart').offset().top;//距离文档顶部的距离
						$('body').append("<img class='addImg' src='"+_this.parents('a').find('img').attr('src')+"' />");
						$('.addImg').css({'left':_this.offset().left+'px','top':_this.offset().top+'px'});
						$('.addImg').animate({'left':left+'px','top':top+'px'},1000,function(){
							$('.addImg').remove();
						})
					}
				}
			});
		} else{
			location.href="login.html";
		}
	})

//取消默认事件
function preDef (evt) {
	var e=evt||window.event;
	if (e.preventDefault) {
		e.preventDefault();
	} else{
		e.returnValue=false;
	}
}
//阻止冒泡的函数
function stopPro (evt) {
	var e=evt||window.event;
	if (e.stopPropagation) {
		e.stopPropagation();//w3c阻止冒泡方法
	} else{
		e.cancelBubble=true;//IE阻止冒泡方法
	}
}